//
//  LZTWelcomeViewController.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/12.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

import UIKit
import SDWebImage

class LZTWelcomeViewController: UIViewController {
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(true)
        
        //更改头像的顶部约束
        self.headImageView.snp_updateConstraints { (make) in
            make.top.equalTo(self.view).offset(100)
        }
        
        //设置动画
        UIView.animateWithDuration(2.0, delay: 1.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { 
            
            //约束更新后，需要再次重新布局 强制刷新UI
            self.view.layoutIfNeeded()
            
            }) { (_) in
                
                UIView.animateWithDuration(0.25, animations: { 
                    
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) in
                        
                        //动画完成时，发送通知告知AppDelegate切换控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(WBNOTIFICATION, object: nil)
                })
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
    }
    
    //设置UI
    private func setupUI() {
        
        //添加控件
        view.addSubview(bgImageView)
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        //添加约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
        headImageView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
        
    }
    
    //懒加载控件
    private lazy var bgImageView: UIImageView = UIImageView(imgName: "ad_background")
    
    //头像
    private lazy var headImageView: UIImageView = {
        
        let img = UIImageView(imgName: "avatar_default_big")
        //调用封装的sd_webimage
        img.lzt_setImageWithString(LZTUserAccountViewModel.sharedTools.userAccount?.avatar_large, placeHolderImgName: "avatar_default_big")
        
        img.layer.cornerRadius = 45
        img.layer.masksToBounds = true
        return img
        
    }()
    
    //欢迎回来 label
    private lazy var messageLabel: UILabel = {
       
        let lab = UILabel()
        lab.text = "欢迎回来"
        lab.font = UIFont.systemFontOfSize(18)
        lab.textColor = UIColor.darkGrayColor()
        lab.textAlignment = .Center
        lab.alpha = 0
        return lab
    }()
}
